/*
 * Copyright 2016-2017 Leon Chen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.moilioncircle.redis.rdb.cli.ext.rct.support;

import static java.util.Arrays.binarySearch;

/**
 * @author Baoyi Chen
 */
public class JemallocCalculator {
	
	public static long malloc(long size) {
		int idx = binarySearch(JEMALLOC_SIZE, size);
		if (idx < 0) idx = -idx - 1;
		return idx < JEMALLOC_SIZE.length ? JEMALLOC_SIZE[idx] : size;
	}
	
	public static final long[] JEMALLOC_SIZE = new long[]{
			8L, 16L, 24L, 32L, 40L, 48L, 56L, 64L, 80L, 96L, 112L, 128L, 160L, 192L, 224L, 256L, 320L, 384L, 448L, 512L, 640L, 768L, 896L, 1024L,
			1280L, 1536L,  1792L, 2048L,  2560L, 3072L, 3584L, 4096L, 5120L, 6144L, 7168L, 8192L, 10240L, 12288L, 14336L, 16384L, 20480L, 24576L,
			28672L, 32768L, 40960L, 49152L, 57344L, 65536L, 81920L, 98304L, 114688L, 131072L, 163840L, 196608L, 229376L, 262144L, 327680L, 393216L,
			458752L,  524288L, 655360L, 786432L, 917504L, 1048576L, 1310720L, 1572864L, 1835008L, 2097152L, 2621440L, 3145728L, 3670016L, 4194304L,
			5242880L, 6291456L, 7340032L, 8388608L, 10485760L, 12582912L, 14680064L, 16777216L, 20971520L, 25165824L, 29360128L, 33554432L,  41943040L,
			50331648L, 58720256L, 67108864L, 83886080L, 100663296L, 117440512L, 134217728L, 167772160L, 201326592L, 234881024L, 268435456L, 335544320L,
			402653184L,  469762048L,  536870912L,  671088640L, 805306368L, 939524096L, 1073741824L, 1342177280L, 1610612736L, 1879048192L, 2147483648L,
			2684354560L,  3221225472L,  3758096384L,  4294967296L,  5368709120L,  6442450944L,  7516192768L,  8589934592L,  10737418240L, 12884901888L,
			15032385536L, 17179869184L, 21474836480L, 25769803776L, 30064771072L, 34359738368L, 42949672960L, 51539607552L, 60129542144L, 68719476736L,
			85899345920L,   103079215104L,  120259084288L, 137438953472L, 171798691840L, 206158430208L,  240518168576L, 274877906944L,   343597383680L,
			412316860416L,  481036337152L,  549755813888L, 687194767360L, 824633720832L, 962072674304L, 1099511627776L, 1374389534720L, 1649267441664L,
			1924145348608L, 2199023255552L, 2748779069440L, 3298534883328L, 3848290697216L, 4398046511104L, 5497558138880L, 6597069766656L, 7696581394432L,
			8796093022208L,   10995116277760L,  13194139533312L,  15393162788864L,  17592186044416L,  21990232555520L,   26388279066624L,  30786325577728L,
			35184372088832L,  43980465111040L,  52776558133248L,  61572651155456L,  70368744177664L,  87960930222080L,  105553116266496L, 123145302310912L,
			140737488355328L, 175921860444160L, 211106232532992L, 246290604621824L, 281474976710656L, 351843720888320L, 422212465065984L, 492581209243648L,
			562949953421312L, 703687441776640L, 844424930131968L, 985162418487296L, 1125899906842624L, 1407374883553280L, 1688849860263936L, 1970324836974592L,
			2251799813685248L,  2814749767106560L,   3377699720527872L,     3940649673949184L,    4503599627370496L,    5629499534213120L,   6755399441055744L,
			7881299347898368L,  9007199254740992L,   11258999068426240L,   13510798882111488L,   15762598695796736L,   18014398509481984L,  22517998136852480L,
			27021597764222976L,   31525197391593472L,   36028797018963968L,   45035996273704960L,   54043195528445952L,     63050394783186944L,   72057594037927936L,
			90071992547409920L,   108086391056891904L,  126100789566373888L,  144115188075855872L,  180143985094819840L,   216172782113783808L,  252201579132747776L,
			288230376151711744L,  360287970189639680L,  432345564227567616L,  504403158265495552L,  576460752303423488L,   720575940379279360L,  864691128455135232L,
			1008806316530991104L, 1152921504606846976L, 1441151880758558720L, 1729382256910270464L, 2017612633061982208L, 2305843009213693952L, 2882303761517117440L,
			3458764513820540928L, 4035225266123964416L, 4611686018427387904L, 5764607523034234880L, 6917529027641081856L, 8070450532247928832L
	};
}
